package cn.edut.utils;

/*
 * JDBC工具类 使用Druid
 * */

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds;

    static {
        try {
            //1.加载配置文件
            Properties p = new Properties();
            //使用ClassLoader加载配置文件，获取字节流
            InputStream resourceAsStream = JdbcTemplate.class.getClassLoader().getResourceAsStream("druid.properties");
            p.load(resourceAsStream);
            //2.初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(p);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * 获取连接池对象
     * */
    public static DataSource getdatasoure() {
        return ds;
    }

    /*
     * 获取连接connection对象
     * */
    public static Connection getconnection() throws SQLException {
        return ds.getConnection();
    }
}
